//
//  RootViewController.m
//  tuangou
//
//  Created by quan jimmy on 11-9-24.
//  Copyright 2011年 tencent. All rights reserved.
//

#import "RecommendViewController.h"
#import "LZClient.h"
#import "SBJson.h"
#import "Category.h"
#import "LZCateCell.h"
#import "LZDealViewController.h"

@implementation RecommendViewController
@synthesize categoryList,HUD,city;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
            }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    LZClient *client = [[LZClient alloc] init];
    [client requestCategory:TAG_CATGORY_LIST delegate:self];
    [client release];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [categoryList count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CategoryCell";
    
    LZCateCell *cell = (LZCateCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[LZCateCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [cell setCate:[self.categoryList objectAtIndex:indexPath.row]];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    Category *cate = [categoryList objectAtIndex:indexPath.row];
    LZDealViewController *dealViewController = [[LZDealViewController alloc] init];
    dealViewController.cateId=cate.id;
    [self.navigationController pushViewController:dealViewController animated:YES];
    
}


//ASIHTTP delegate
- (void)requestFinished:(ASIHTTPRequest *)request
{
    if (request.responseStatusCode != 200) {
        [self requestErrorHandle:request.responseStatusCode message:@"http error" tag:request.tag];
        return;
    }
    NSMutableArray *cats = [[NSMutableArray alloc] init];
    NSString *response = [request responseString];
    
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSArray *rows = [parser objectWithString:response];
    if (!rows) {
        [cats release];
        [parser release];
        return;
    }
    for (NSDictionary *row in rows) {
        Category *cat = [[[Category alloc] init] initWithDict:row];
        [cats addObject:cat];
        [cat release];
    }
    [parser release];
    
    if (request.tag == TAG_CATGORY_LIST) {
        
        categoryList = cats;
        [categoryList retain];
        [HUD hide:YES];
        [self.tableView reloadData]; 

    }
    
    [cats release];
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"error: %@", error);
    [self requestErrorHandle:request.responseStatusCode message:@"http error" tag:request.tag];
}

- (void)requestErrorHandle:(NSInteger)errorCode message:(NSString *)errorMessage tag:(NSInteger)tag {
    if (tag == TAG_CATGORY_LIST) {
        [HUD hide:YES];
        categoryList = nil;
        [self.tableView reloadData];
    }
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

@end
